package com.usian.admin.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("ALL")
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {

    /**
     * 查询  分页  查询条件
     * @param dto 封装dto 传递对象
     * @return 通用返回对象
     */
    @Override
    public PageResponseResult findPage(ChannelDto dto) {
        //  1 判断 dto对象 是否为空 如果为空 提示
        if (dto==null){
            return  (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2 初始化 分页参数
        dto.checkParam();
        // 3 封装查询条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(dto.getName()!=null&&!"".equals(dto.getName()),AdChannel::getName,dto.getName());
        // 4 执行分页结果
        Page<AdChannel> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<AdChannel> ipage = this.page(page, lambdaQueryWrapper);
        //5 封装分页结果
        PageResponseResult  tResponseResult = new PageResponseResult(ipage.getCurrent(), ipage.getSize(), ipage.getTotal());
        tResponseResult.setData(ipage.getRecords() );
        //6 返回
        return tResponseResult;
    }

    /**
     *  添加
     * @param adChannel 封装对象
     * @return
     */
    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {
        // 先判断
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getName()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 添加
        // 维护初始化字段
        if (adChannel.getCreatedTime()==null){
            adChannel.setCreatedTime(new Date());
        }

        this.save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     *  根据ID 修改数据
     * @param adChannel 封装对象
     * @return
     */
    @Override
    public ResponseResult update( AdChannel adChannel) {
//        // 1 判断 参数 是否为空
//        if (id==null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2 查询是否有修改对象
        AdChannel byId = this.getById(adChannel.getId());
        // 3 没有修改
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        //4 有修改
//        adChannel.setId(id);
        this.saveOrUpdate(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     *  根据ID 删除数据
     * @param id  传递ID
     * @return 返回结果
     */
    @Override
    public ResponseResult del(Integer id) {
        // 1 判断 参数 是否为空
        if (id==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //  直接删除
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult findAll() {
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdChannel::getStatus,1);
        lambdaQueryWrapper.orderByDesc(AdChannel::getOrd);
        List<AdChannel> list =  this.list(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }

}
